package csse3004.GD.PAdmin;

/**
 * @author Mamdouh Alharbi- s4194298
 */
import csse3004.GD.DA.PollAdminDA;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import javax.servlet.ServletException;

public class NewPCController extends SimpleFormController {

    private PollAdminService pollAdminService = new PollAdminDA();

    @Override
    public ModelAndView onSubmit(Object command) throws ServletException {
        NewPACreator pollName = (NewPACreator) command;
        String name = pollName.getName();
        if (name != null) {
            int userID = Integer.parseInt(pollName.getUserID());
            logger.info(userID);
            pollAdminService.createPoll(name, userID); 

            logger.info("in the controller: " + name);
            String created = "Poll is created successfully";

            return new ModelAndView("home", "created", created);
        }
        return new ModelAndView(new RedirectView(getSuccessView()));
    }
}